package com.joint.base.mp;

import com.fz.us.base.service.memcached.CacheConsts;
import com.fz.us.base.service.memcached.SpyMemcachedClient;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import org.xml.sax.InputSource;

import javax.annotation.Resource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.InputStream;

/**
 * @author min_xu
 *
 * 微信配置
 */
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WxMpInCacheConfigStorage extends WxMpInMemoryConfigStorage {
  private final static String wxMpConfigStorage_token = CacheConsts.WXMP_ACCESS_TOKEN_TEXT;

    //访问微信的域名 没有http://
    protected String host;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    protected String version;
    protected static String http = "http://";
    public String getHttpHost() {
        return http+host;
    }
    public void setHttpHost(String httpHost) {
        this.httpHost = httpHost;
    }
    protected  String httpHost;

  @Resource
  private SpyMemcachedClient spyMemcachedClient;

  @Override
  public String toString() {
    return "SimpleWxConfigProvider [appId=" + appId + ", secret=" + secret + ", accessToken=" + accessToken
        + ", expiresIn=" + expiresIn + ", token=" + token + ", aesKey=" + aesKey + "]";
  }


  public static WxMpInCacheConfigStorage fromXml(InputStream is) throws JAXBException {
    Unmarshaller um = JAXBContext.newInstance(WxMpInCacheConfigStorage.class).createUnmarshaller();
    InputSource inputSource = new InputSource(is);
    inputSource.setEncoding("utf-8");
    return (WxMpInCacheConfigStorage) um.unmarshal(inputSource);
  }

    @Override
    public void updateAccessToken(String accessToken, int expiresIn) {
        this.setAccessToken(accessToken);
        this.setExpiresIn(expiresIn);

        setTokenToCache(accessToken, expiresIn);
    }

    @Override
    public String getAccessToken(){
        return getTokenFromCache();
    }

    public String getTokenFromCache(){
          String _accessToken = spyMemcachedClient.get(wxMpConfigStorage_token +"#" + getAppId());
          this.setAccessToken(_accessToken);
          return _accessToken;
    }

    public void setTokenToCache(String accessToken, int expiresIn){
        this.setAccessToken(accessToken);
        spyMemcachedClient.set(wxMpConfigStorage_token + "#" + getAppId(), expiresIn, accessToken);
    }

    public void deleteTokenFromCache(){
        this.setAccessToken("");
        spyMemcachedClient.delete(wxMpConfigStorage_token + "#" + getAppId());
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

}
